// //
// // Created by lfm on 25-4-8.
// //

#include <iostream>
using namespace std;
template<class T>

class MyArray
{
public:
    //有参构造
    MyArray(int capacity)
    {
        //cout << "MyArray的有参构造调用" << endl;
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }
    //尾插法
    void Push_Back(const T & value)
    {
        if (this->m_Capacity == this->m_Size)
        {
            return ;
        }
        this->pAddress[this->m_Size] = value;
        this->m_Size++;
    }
    void Pop_Back()
    {
        if (this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;
    }
    //通过下标访问数组中的元素
    //作为左值返回引用
    T &operator[](int index)
    {
        return this->pAddress[index];
    }
    ~MyArray()
    {
        //cout << "MyArray的析构调用" << endl;
        if (this->pAddress != nullptr)
        {
            delete[] this->pAddress;
            pAddress = nullptr;
        }
    }
    //拷贝构造
    MyArray(const MyArray& arr)
    {
        ///cout << "MyArray的拷贝调用" << endl;
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;

        //深拷贝
        this->pAddress = new T[arr.m_Capacity];
        //将Array中的数据都拷贝过来
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    //operator=防止浅拷贝问题
    MyArray & operator=(const MyArray& arr)
    {
        //cout << "MyArray的重载调用" << endl;
        //先判断原来堆区是否释放，如果有先释放
        if (this->pAddress != nullptr)
        {
            delete[]this->pAddress;
            this->pAddress = nullptr;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }
        //深拷贝
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[arr.m_Capacity];
        return *this;
    }
    //返回数组容量
    int GetCapacity()
    {
        return this->m_Capacity;
    }
    //返回数组大小
    int getSize()
    {
        return this->m_Size;
    }
private:
    T* pAddress;//指针指向堆区开辟的真实数组

    int m_Capacity;//数组容量

    int m_Size;//数组大小

};